package com.japp.manager.service;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.japp.base.BaseService;
import com.japp.manager.bean.RiskDealBean;
import com.japp.manager.mapper.RiskDealMapper;
import com.japp.tools.common.JappLog;
import com.japp.tools.page.Page;

/**
 * 研究报告
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RiskDealService extends SqlSessionDaoSupport implements BaseService {
	

	@Autowired
	private SqlSessionTemplate template;
	
	@Autowired
	private RiskDealMapper riskDealMapper;

	public RiskDealBean get(Serializable pk) {
		return null;
	}

	public Page<RiskDealBean> getPage(Page<RiskDealBean> po, RiskDealBean obj) {
		try {
			Page<RiskDealBean> page = riskDealMapper.getPage(po, obj);
			return page;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public List<RiskDealBean> queryRiskDealAll() {
		try {
			return riskDealMapper.queryRiskDealAll();
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}
	
	public int insertRiskDealList(List<RiskDealBean> list){
		int rel = 0;
		try {
			rel = riskDealMapper.insertRiskDealList(list);
		} catch (Exception e) {
			rel = 0;
			e.printStackTrace();
		}
		return rel;
	}
	
	

	public RiskDealBean findRiskDealById(int rdId) {
		try {
			return riskDealMapper.findRiskDealById(rdId);
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public int insertRiskDeal(List<RiskDealBean> rd) {
		int rel = 0;
		try {
			for(RiskDealBean r:rd ){
				r.setDate_type("0");
				rel += riskDealMapper.insertRiskDeal(r);
			}
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	public int deleteRiskDeal(String[] keys) {
		try {
			int rel = riskDealMapper.deleteRiskDeal(keys);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	public int updateRiskDeal(RiskDealBean rd) {
		try {
			int rel = riskDealMapper.updateRiskDeal(rd);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}
	
	public Map<String, String> getUserByUcode(Set<String> set){
		Map<String, String> map = new HashMap<String, String>();
		Connection conn = null;
		Statement sta = null;
		ResultSet rs = null;
		String sql = "";
		try {
			conn = template.getSqlSessionFactory().openSession().getConnection();
			for (String str : set) {
				JappLog.log.info(str);
				sql += "SELECT '"+str+"' SUBMIT_UCODE" +
						",U_NAME SUBMIT_UNAME" +
						",U_LTD SUBMIT_DPID" +
						",(SELECT DP_NAME FROM TB_DP WHERE DP_ID = U_LTD) SUBMIT_DPNAME" +
						" FROM TB_USER WHERE U_CODE = '"+str+"' UNION ";
			}
			sql = sql.substring(0,sql.length()-6);
			sta = conn.createStatement();
			rs = sta.executeQuery(sql);
			while(rs.next()){
				map.put(rs.getString("submit_ucode"), rs.getString("submit_dpid")+"!"+rs.getString("submit_dpname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(sta!=null)sta.close();
				if(rs!=null)rs.close();
				if(conn!=null)conn.close();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
		}
		return map;
	}
	


}
